/*
 * FileTest.c
 *
 *  Created on: Aug 29, 2008
 *      Author: V
 *
 * Version 1.1
 *
 * Version History:
 * 1.0	Last copy after completion of HW1 in the HW1 project.
 * 1.1  Updated to use new Object_method convention.
 */

#include "../file/File.h"

#include "CuTest.h"

#include "../gc6.7/include/gc.h"

#include <assert.h>


void TestFileNew(CuTest* ct)
{
	pFile file = FileNew(String_new("tests/TestSingleLineFile.txt"), String_new("r"));
	CuAssertTrue(ct, file->fileHandle != NULL);
	CuAssertStrEquals(ct, "tests/TestSingleLineFile.txt", file->filename->cString);
	CuAssertStrEquals(ct, "r", file->options->cString);
	CuAssertTrue(ct, file->fileHandle != NULL);
}

void TestFile_Create(CuTest* cu)
{
	pFile file = File_Create("tests/testingFileCreate.txt", "abcde");
	CuAssertTrue(cu, file != NULL);
	CuAssertIntEquals(cu, 5, File_GetSize(file));
	File_Delete(file);
}

void TestFile_Read(CuTest* ct)
{
	pFile file = FileNew(String_new("tests/TestSingleLineFile.txt"), String_new("r"));
	pString contents = File_Read(file);
	assert(contents != NULL);
	CuAssertStrEquals(ct, "Single Line Test.", contents->cString);

	file = FileNew(String_new("tests/TestMultilineFile.txt"), String_new("r"));
	contents = File_Read(file);
	assert(contents != NULL);
	pString expected = String_new("Test Multiline File.\r\nTest Line 2.\r\nTest Line 3.");
	CuAssertStrEquals(ct, expected->cString, contents->cString);
}

void TestFile_Append(CuTest* ct)
{
	// init major variables
	pString filename = String_new("tests/testingAppend.txt");
	pFile file = FileNew(filename, String_new("a"));
	pString toAppend = String_new("HelloWorld");
	int isNotError;

	// append to empty file
	isNotError = File_Empty(file);
	assert(isNotError);

	// append once
	isNotError = File_Append(file, toAppend);
	assert(String_indexOf(file->options, "a") != -1);
	assert(isNotError);
	File_Close(file);

	// check
	file = FileNew(filename, String_new("r"));
	pString actual = File_Read(file);
	CuAssertTrue(ct, actual != NULL);
	CuAssertStrEquals(ct, toAppend->cString, actual->cString);
	File_Delete(file);

	// append three times and check
	File_Empty(file);
	File_Close(file);
	file = FileNew(filename, String_new("a"));
	File_Append(file, toAppend);
	File_Append(file, toAppend);
	File_Append(file, toAppend);
	File_Close(file);
	file = FileNew(filename, String_new("r"));
	actual = File_Read(file);
	CuAssertStrEquals(ct, "HelloWorldHelloWorldHelloWorld", actual->cString);
	File_Delete(file);


}

void TestFile_Empty(CuTest* cu)
{
	pFile file = FileNew(String_new("tests/testingEmpty.txt"), String_new("a"));
	assert(file);
	int isNotError;
	isNotError = File_Append(file, String_new("HelloEmpty"));
	assert(isNotError);
	isNotError = File_Empty(file);
	assert(isNotError);
	CuAssertIntEquals(cu, 0, File_GetSize(file));
	File_Delete(file);
}

void TestFile_GetSize(CuTest* cu)
{
	pFile file = File_Create("tests/testingGetSize.txt", "0123456789");
	int size = File_GetSize(file);
	CuAssertIntEquals(cu, 10, size);
	CuAssertStrEquals(cu, "tests/testingGetSize.txt",  file->filename->cString);
	int errorNo = File_Delete(file);
	CuAssertIntEquals(cu, 1, errorNo);
}

void TestFile_Close(CuTest* cu)
{
	pString filename = String_new("tests/testingFileClose.txt");
	pFile file = FileNew(filename, String_new("a+"));
	int isNotError = File_Close(file);
	CuAssertTrue(cu, isNotError);
	pString readFile = File_Read(file);
	CuAssertTrue(cu, readFile != NULL);
	CuAssertStrEquals(cu, "\0", readFile->cString);
	File_Delete(file);
}
